#include "Hweather.h"

const String wea_host = "https://devapi.heweather.net/v7/weather/now?gzip=n&location=";
const String city_url = "https://geoapi.qweather.com/v2/city/lookup?gzip=n&lang=en&location=";
const String future_url = "https://devapi.qweather.com/v7/weather/3d?gzip=n&location=";

Hweather hweather;


void _parseNowJson(String payload) 
{
  const size_t capacity = 2*JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(5) +
                          JSON_OBJECT_SIZE(15) + 350;
  DynamicJsonDocument doc(capacity);

  deserializeJson(doc, payload);

  JsonObject now = doc["now"];
  hweather.temp = now["temp"].as<int>();                // 实况温度
  hweather.feel = now["feelsLike"].as<int>();      // 实况体感温度
  hweather.icon = now["icon"].as<int>();                // 当前天气状况和图标的代码
  hweather.weather = now["text"].as<String>();             // 实况天气状况的文字描述
  hweather.wind = now["windDir"].as<String>();       // 实况风向
  hweather.humidity = now["humidity"].as<float>();    // 实况相对湿度百分比数值
  #ifdef DEBUG
  {
    Serial.printf("温度:%d,湿度:%d,体感:%d,图标:%d,天气:",hweather.temp,hweather.humidity,hweather.feel,hweather.icon);
    Serial.print(hweather.weather);
    Serial.print(",风向");
    Serial.print(hweather.wind);
  }
  #endif

}


bool Hweather::get_weather(String location) 
{
    String wea_url = wea_host + location + UserKey;
    #ifdef DEBUG
    {
        Serial.println("start get now hweather");
        Serial.print("URL=");
        Serial.println(wea_url);
    }
    #endif
    String payload = http_get(wea_url,1);
    if(strlen(&payload[0])>100)
    {
        #ifdef DEBUG
        {
            Serial.println("get success");
            Serial.print("code=");
            Serial.println(payload);
        }
        #endif
        _parseNowJson(payload);
        return 1;
    }
    else
    {
      #ifdef DEBUG
      {
        Serial.println("get fail");
      }
      #endif
      return 0;
    }
}



//解析Json信息
void city_json(String payload)
{
  char *p=&payload[0];
  char i;
  p+=25;
  while(*p++)
  {
    if(*p=='n'&&*(p+1)=='a')
    {
      p+=7;
      break;
    }
  }
  hweather.location=*p++;
  while(*p)
  {
    if(*p!='"'&&*p!=' ')
      hweather.location+=*p++;
    else
    {
      break;
    }
      
  }
  while(*p++)
  {
    if(*p=='i'&&*(p+1)=='d')
    {
      p+=5;
      break;
    }
  }
  hweather.locationID=*p++;
  while(*p)
  {
    if(*p!='"')
      hweather.locationID+=*p++;
    else
    {
      break;
    }  
  }
  #ifdef DEBUG
  {
    Serial.print("location=");
    Serial.println(hweather.location);
    Serial.print("locationID=");
    Serial.println(hweather.locationID);
  }
  #endif
  
}

bool Hweather::get_city(String longitude,String latitude)
{
    return get_city(longitude + "," + latitude);
}

bool Hweather::get_city(String location)
{

  String wea_url = city_url + location + UserKey ;
  #ifdef DEBUG
  {
    Serial.println("start get hweather city ID");
    Serial.print("URL=");
    Serial.println(wea_url);
  }
  #endif
  String payload = http_get(wea_url,1);
  if(strlen(&payload[0])>100)
  {
    #ifdef DEBUG
    {
        Serial.println("get success");
        Serial.print("code=");
        Serial.println(payload);
    }
    #endif
    city_json(payload);
    return 1;
  }
  else
  {
    #ifdef DEBUG
    {
        Serial.println("get fail");
    }
    #endif
    return 0;
  }
}

// 解析Json信息



void future_json(String payload) {
  const size_t capacity = 2*JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) +
                          JSON_OBJECT_SIZE(5) + 3*JSON_OBJECT_SIZE(26) + 1320;
  DynamicJsonDocument doc(capacity);

  deserializeJson(doc, payload);

  const char* code = doc["code"];
  const char* updateTime = doc["updateTime"];
  JsonArray daily = doc["daily"];
  JsonObject daily_0 = daily[0];
  JsonObject daily_1 = daily[1];
  JsonObject daily_2 = daily[2];


  hweather.tempMax[0] = daily_0["tempMax"].as<int>();
  hweather.tempMax[1] = daily_1["tempMax"].as<int>();
  hweather.tempMax[2] = daily_2["tempMax"].as<int>();
  hweather.tempMin[0] = daily_0["tempMin"].as<int>();
  hweather.tempMin[1] = daily_1["tempMin"].as<int>();
  hweather.tempMin[2] = daily_2["tempMin"].as<int>();

  hweather.textDay[0] = daily_0["textDay"].as<String>();
  hweather.textDay[1] = daily_1["textDay"].as<String>();
  hweather.textDay[2] = daily_2["textDay"].as<String>();

  #ifdef DEBUG
  {
    for (u8 i = 0; i < 3; i++) {
      Serial.print("温度:");
      Serial.print(hweather.tempMin[i]);
      Serial.print("-");
      Serial.print(hweather.tempMax[i]);
      Serial.println("℃");
      Serial.print("天气:");
      Serial.println(hweather.textDay[i]);
    }
  }
  #endif
  

}



bool Hweather::get_future_weather(String location) {
    String url = future_url + location + UserKey;
    #ifdef DEBUG
    {
        Serial.println("start get future hweather");
        Serial.print("URL=");
        Serial.println(url);
    }
    #endif
    String payload = http_get(url,1);

    if(strlen(&payload[0])>100)
    {
        #ifdef DEBUG
        {
            Serial.println("get success");
            Serial.print("code=");
            Serial.println(payload);
        }
        #endif
        future_json(payload);
        return 1;
    }
    else
    {
      #ifdef DEBUG
      {
        Serial.println("get fail");
      }
      #endif
      return 0;
    }
}

